<?php
class Special extends common{
    
    public function index(){
        $mod=&D('Special');
        $sp=$mod->select(array(
            'where'=>array('is_del'=>0),
        ));
        $this->assgin('sp', $sp);
        $this->display();
    }
    
    public function add(){
    	$spid=isset($_GET['spid'])?$_GET['spid']:'';
        $mod=&D('Category');
        $cates=$mod->select(array(
            'where'=>array('is_del'=>0),
            'orderby'=>'pid'
        ));
        $cate=array();
        foreach ($cates as $ck=>$cv){
            if ($cv['pid']==0){
                $cate[$cv['id']]=$cv['cate_name'];
            }
        }

/*        $pos=array();
        $npos=array();
        foreach ($cates as $ck=>$cv){
            if ($cv['pid']==0){
                $pos[$cv['id']]=$cv['id'].','.$cv['cate_name'].'|';
            }else {
                $str=$pos[$cv['pid']];
                $npos[$cv['id']]=$str.$cv['id'].','.$cv['cate_name'].'|';
                if (!array_key_exists($cv['pid'], $pos)){
                    $pos=$npos;
                    $npos=array();
                    $npos[$cv['id']]=$str.$cv['id'].','.$cv['cate_name'].'|';
                }
            }
        }var_dump($npos, $cv['pid']);exit;*/
        
        //专题
        $sp=array('id'=>'','sp_name'=>'','sp_logo'=>'','orderby'=>'','is_poster'=>'','is_cate'=>'','push_time'=>time());
        $ps=array();
        $photos=array();
        if (!empty($spid)){
    	  $sp_mod=&D('Special');
    	  $sp=$sp_mod->find(array(
    	      'where'=>array('is_del'=>0,'id'=>$spid)
    	  ));
    	  
    	  $ps_mod=&D('PhotoSpecial');
    	  $ps=$ps_mod->r_select(array(
    	      'join'=>array(array('Photos','','id')),
    	      'pk'=>'photo_id',
    	      'where'=>array('special_id'=>$spid)
    	  ));
    	  //var_dump($ps);exit('jj');
        }
        $this->assgin('ps', $ps);
        $this->assgin('sp', $sp);
        $this->assgin('cate', $cate);
        $this->display();
    }
    
    public  function edit(){
    	$id=$_GET['id'];
    	$sp_mod=&D('Special');
    	$sp=$sp_mod->find(array(
    	    'where'=>array('is_del'=>0,'id'=>$id)
    	));
    	$this->assgin('sp', $sp);
    	$this->display();
    }
    
    public function doAdd(){
        $mod=&D('Special');
        $data['sp_name']=$_POST['name'];
        $data['sp_logo']=$_POST['img'];
        $data['orderby']=$_POST['orderby'];
        $data['is_poster']=isset($_POST['poster'])?$_POST['poster']:0;
        $data['is_cate']=isset($_POST['showCate'])?$_POST['showCate']:0;
        $data['push_time']=empty($_POST['pushTime'])?0:strtotime($_POST['pushTime']);
        $data['add_time']=time();
        if (empty($_GET['spid'])){
          if ($spId=$mod->add($data)){
            if (isset($_POST['photov']) && !empty($_POST['photov'])){
            	$_POST['photov']=explode(',', trim($_POST['photov'],','));
                foreach ($_POST['photov'] as $pv){
                  $ps_mod=&D('PhotoSpecial');
                  $ps_mod->add(array('photo_id'=>$pv,'special_id'=>$spId));
                }
            }
            echo '添加成功,1秒钟后自动跳转';
        }else{ 
            echo '添加失败,1秒钟后自动跳转';
        }
            
        header('refresh:1;url="?m=special&a=add"');
      }else {
      	$spId=$_GET['spid'];
      	 $mod->update(array(
      	     'where'=>array('id'=>$spId),
      	     'data'=>$data
      	 ));
      	 $ps_mod=&D('PhotoSpecial');
      	 $ps_mod->delete(array(
      	     'where'=>array('special_id'=>$spId)
      	 ));
         if (isset($_POST['photov']) && !empty($_POST['photov'])){
             $_POST['photov']=explode(',', trim($_POST['photov'],','));
             foreach ($_POST['photov'] as $pv){
                  $ps_mod->add(array('photo_id'=>$pv,'special_id'=>$spId));
             }
         }
      	 echo '更新成功！系统将会自动跳转';
      	 header('refresh:1;url="?m=special"');
      }
    
    
    
    }
    
    public function getCate(){
       $cateId=$_GET['id'];
       $cateId==0 && $cateId=-1;
       $mod=&D('Category');
       $cate=$mod->select(array(
               'field'=>'id,cate_name',
               'where'=>array('is_del'=>0,'pid'=>$cateId),
               'orderby'=>'pid'
            ));
      $str="{'data':[";
      foreach ($cate as $cv){
          $str.="{'id':{$cv['id']},'name':'{$cv['cate_name']}'},";
      }
      exit(rtrim($str,',').']}');
    
    }
    
    public function getPhoto(){
        $cateId=$_GET['id'];
        $mod=&D('PhotoCate');
        $photos=$mod->r_select(array(
            'where'=>array('cate_id'=>$cateId),
            'field'=>'photo_id',
            'join'=>array(array('Photos','id,photo_name,photo_small','id')),
            'pk'=>'photo_id'
        ));
        $str='{"photo":[';
        foreach ($photos as $pk=>$pv){
            $str.="{'id':{$pv['id']},'photo_name':'{$pv['photo_name']}','photo_small':'{$pv['photo_small']}'},";
        }
        exit(rtrim($str,',').']}');
    }
    
    
    public function doUpload(){
        import('upload');
        $upload=new Upload('spfile');
        $upload->path('special');
        $upload->doUpload();
        echo '{img:"'.$upload->getFile().'"}';
    }
    
    
}